package com.unisoc.systemui.power;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.WindowManager;
import android.os.BatteryManager;
import android.util.Log;

import com.android.systemui.R;

public class UnisocPowerUI {

    private static final String TAG = "UnisocPowerUI";
    private Context mContext = null;
    private AlertDialog mVoltageHighDialog = null;

    public UnisocPowerUI(Context context){
        mContext = context;
    }

    public void checkBatteryHealth(boolean plugged,boolean oldPlugged,int mBatteryHealth,int oldBatteryHealth){
        /* SPRD: Modified for bug 505221, add voltage high warning @{ */
        if (plugged) {
            if (oldBatteryHealth != mBatteryHealth && oldBatteryHealth != BatteryManager.BATTERY_HEALTH_UNKNOWN) {
                if (mBatteryHealth == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
                    Log.d(TAG, "showing BATTERY_HEALTH_OVER_VOLTAGE warning");
                    showVoltageHighWarningDialog();
                } else if (oldBatteryHealth == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
                    Log.d(TAG, "dismiss BATTERY_HEALTH_OVER_VOLTAGE warning");
                    dismissVoltageHighWarningDialog();
                }
                else {
                     // do nothing
                }
            }
        } else {
            if (oldPlugged) {
                dismissVoltageHighWarningDialog();
            }
        }
        /* @} */
    }

    /* SPRD: Modified for bug 505221, add voltage high warning @{ */
    private void showVoltageHighWarningDialog() {
        Log.d(TAG, "showing showVoltageHighWarningDialog dialog");
        AlertDialog.Builder b = new AlertDialog.Builder(mContext);
        b.setTitle(R.string.battery_overtage_title);
        b.setCancelable(false);
        b.setMessage(R.string.battery_overtage_content);
        b.setIconAttribute(android.R.attr.alertDialogIcon);
        b.setPositiveButton(R.string.battery_overtage_back,
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
        }
        });

        AlertDialog d = b.create();
        d.setOnDismissListener(new DialogInterface.OnDismissListener() {
            public void onDismiss(DialogInterface dialog) {
                mVoltageHighDialog = null;
            }
        });

        d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
        d.show();
        mVoltageHighDialog = d;
    }

    private void dismissVoltageHighWarningDialog() {
        if (mVoltageHighDialog != null) {
            Log.d(TAG, "dismiss dismissVoltageHighWarningDialog dialog");
            mVoltageHighDialog.dismiss();
        }
    }
    /* @} */
}
